using MediatR;
using SmartApplication.Api.Applient.Command.Feed;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Feed
{
    public class UpdateCompaniesHandler : IRequestHandler<UpdateCompaniesCommand, ApiResult<int>>
    {
        public UpdateCompaniesHandler(IBaseRepository<CompaniesModel> companiesRepository)
        {
            CompaniesRepository = companiesRepository;
        }

        public IBaseRepository<CompaniesModel> CompaniesRepository { get; }

        /// <summary>
        /// 修改养殖企业信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> Handle(UpdateCompaniesCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();

            var companies = await CompaniesRepository.GetValueAsync(request.Id);
            companies.CompaniesName = request.CompaniesName;
            companies.ChargeName = request.ChargeName;
            companies.AnimalNumber = request.AnimalNumber;
            companies.Pic = request.Pic;

            var update = await CompaniesRepository.UpdateAsync(companies);
            if (update > 0)
            {
                res.Code = EnumCode.Success;
                res.Message = "修改养殖企业信息成功";
                res.Data = update;
                return res;
            }
            else
            {
                res.Code = EnumCode.Fail;
                res.Message = "修改养殖企业信息失败";
                res.Data = update;
                return res;
            }
        }
    }
}